<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyActivity extends BaseModel
{
    use SoftDelete;
    protected $name = 'sky_activity';

    /**
     * 日历
     * @param $date
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function calendar($date){
        $tagName = (new SkyTag())->tagName();
        $activity = $this->where('end_time','>=',time())
            ->where('status','=',1)->order('sort desc')->cache(true)->select()->toArray();
        $season = (new SkyCategory())->where('category','=','季节先祖')
            ->cache(true)
            ->column('sub_category');
        $return = [];
        foreach($activity as $item){
            $target = '';
            $type = '';
            $target_season = '';

            if(!empty($item['target'])){
                $target = $item['target'];
                $type = 1;
            }else{
                if(!empty($item['target_season'])){
                    $target_season = $item['target_season'];
                    if(in_array($item['target_season'],$season)){
                        $type = 2;
                    }else{
                        $type = 3;
                    }
                }
            }

            $return['list'][] = array(
                'icon'    => $item['icon'],
                'name'    => $item['activity_name'],
                'tagName' => time() >= $item['start_time']?$tagName[$item['now_tag']]['tag']:$tagName[$item['tag']]['tag'],
                'tagBackgroundColor' => time() >= $item['start_time']?$tagName[$item['now_tag']]['color']:$tagName[$item['tag']]['color'],
                'tagTextColor' => time() >= $item['start_time']?$tagName[$item['now_tag']]['word_color']:$tagName[$item['tag']]['word_color'],
                'startDate'    => date('Y-m-d H:i:s',$item['start_time']),
                'endDate'      => date('Y-m-d H:i:s',$item['end_time']),
                'intro'       => array(
                    'spiritID'       => $target,
                    'spiritSeason'   => $target_season,
                    'spiritType'     => $type,
                    'title'          => $item['activity_name'],
                    'desc'   => str_replace(PHP_EOL,'</br>',$item['activity_desc']),
                    'images' => json_decode($item['activity_img'],true),
                )
            );
        }

        //今日魔法
        $return['magic'] = (new SkyMagic())->todayMagic($date);
        //今日任务
        $return['task'] = (new SkyTask())->todayTask($date);

        return array_merge($return,(new SkyCandle())->todayCandle($date));
    }
}